﻿using AutoMapper;
using B.S.GridManage.Api.Write.Application.Command.LogicalGrid;
using B.S.GridManage.Domain.Entities;
using B.S.GridManage.ErrorCode;
using B.S.GridManage.Infrastructure.Interface;
using MediatR;
using Yitter.IdGenerator;

namespace B.S.GridManage.Api.Write.Application.Handler.LogicalGrid
{
    public class DelLogicalGridHandler : IRequestHandler<DelLogicalGridCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<LogicalGridModel> logicalRepo;
        private readonly IMapper mapper;
        private readonly ILogger<DelLogicalGridHandler> logger;

        public DelLogicalGridHandler(IBaseRepository<LogicalGridModel> logicalRepo,IMapper mapper ,ILogger<DelLogicalGridHandler> logger)
        {
            this.logicalRepo = logicalRepo;
            this.mapper = mapper;
            this.logger = logger;
        }
        /// <summary>
        /// 删除逻辑网格的处理方法
        /// </summary>
        /// <param name="request">请求命令</param>
        /// <param name="cancellationToken">取消参数</param>
        /// <returns></returns>

        public async Task<ApiResult<int>> Handle(DelLogicalGridCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> res = new ApiResult<int>();//实例化响应包装类

           
            //非空校验

            var model =  await logicalRepo.GetModel(request.Id);
            if (model == null) {
                res.Msg = "您要修改的逻辑网格不存在!";
                res.Code = ApiEnum.Failed;
                return res;//不要忘了返回
            }

            model.IsDeleted = true;

            int r = await logicalRepo.UpdateAsync(model);
            if (r > 0)
            {
                res.Msg = "删除逻辑网格成功!";
                res.Code = ApiEnum.Success;
                res.Data = r;
            }
            else
            {
                res.Msg = "删除逻辑网格失败!";
                res.Code = ApiEnum.Failed;
                res.Data = r;
            }
            return res;


        }
    }
}
